<?php
/**
 * Created by ThinkPHP5.0.22.
 * User: 小涛.
 * Date: 2018/12/27.
 * Time: 22:15.
 */

namespace app\admin\controller;

use app\admin\model\NewsType as NewsTypeModel;
use think\Request;

/**
 * Class NewsType 资讯类型列表
 * @package app\admin\controller
 */
class NewsType extends Admin
{
    //列表
    public function index(){
        $list = NewsTypeModel::listNewsType(10);
        $this->assign("list",$list);
        return $this -> fetch();
    }

    //添加
    public function add(){
        $request = Request::instance();
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data = $request -> param();
            $data["update_time"] = time();
            $data["create_time"] = time();
            $add = new NewsTypeModel($data);
            if($add -> allowField(true) -> save()){
                return "添加成功";
            }
            return "添加失败";
        }
        return $this -> fetch();
    }

    //排序
    public function sort(){
        $request = Request::instance();
        if($request -> isPost()){
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            $data = $request -> param();
            $id = 0;
            if(array_key_exists("id",$data)){
                $id = $data["id"];
            }
            $data["update_time"] = time();
            $edit = new NewsTypeModel();
            $edit = $edit -> allowField(true) -> save($data, ["id" => $id]);
            if($edit){
                return "排序成功";
            }
            return "排序失败";
        }
    }

    //删除
    public function delete(){
        $request = Request::instance();
        if($request -> isPost()){
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            $id = 0;
            $data = $request -> param();
            if(array_key_exists("id",$data)){
                $id = $data["id"];
            }
            $delete = new NewsTypeModel();
            $delete = $delete -> where(["id" => $id]) -> delete();
            if($delete){
                return "删除成功";
            }
            return "删除失败";
        }
    }

    //编辑
    public function edit(){
        $request = Request::instance();
        $data = $request -> param();
        $id = 0;
        if(array_key_exists("id",$data)){
            $id = $data["id"];
        }
        $list = NewsTypeModel::get($id);
        if ($request -> isPost()) {
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            if(strtotime($list["create_time"]) == 0){
                $data["create_time"] = time();
            }
            $data["update_time"] = time();
            $edit = new NewsTypeModel();
            $edit = $edit -> allowField(true) -> save($data,["id" => $id]);
            if($edit){
                return "编辑成功";
            }
            return "编辑失败";
        }
        if(!$list){
            $this->redirect("admin/NewsType/Index");
        }
        $this->assign("list",$list);
        return $this -> fetch();
    }
}